home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / chmod.c < prev    next >
C/C++ Source or Header  |  1993-09-22  |  2KB  |  85 lines

  1. /* chmod -- change the permissions of a file */
  2. /* chown -- change the owner and group of a file */
  3. /* written by Eric R. Smith and placed in the public domain */
  4.  
  5. #include <types.h>
  6. #include <stat.h>
  7. #include <osbind.h>
  8. #include <mintbind.h>
  9. #include <limits.h>
  10. #include <errno.h>
  11. #include <unistd.h>
  12. #include "lib.h"
  13.  
  14. extern int __mint;
  15.  
  16. int chmod(_path, mode)
  17.        const char *_path;
  18.        int   mode;
  19. {
  20.        int  dosattrib = 0, r;
  21.        char path[PATH_MAX];
  22.  
  23.        (void)_unx2dos(_path, path);
  24.  
  25.     if (__mint >= 9) {    /* use MiNT Fchmod function */
  26.         r = (int)Fchmod(path, mode);
  27.         if (r) {
  28.             errno = -r;
  29.             return -1;
  30.         }
  31.         return 0;
  32.     }
  33.  
  34. /* The following lines ensure that the archive bit isn't cleared */
  35.     r = Fattrib(path, 0, 0);
  36.     if (r < 0)
  37.       {
  38.         errno = -r;
  39.         return -1;
  40.       }
  41.      if (r & FA_CHANGED)
  42.         dosattrib |= FA_CHANGED;
  43.  
  44.     if (r & FA_DIR)
  45.       dosattrib |= FA_DIR;
  46. #if 0
  47.        if (!(mode & S_IREAD))
  48.                dosattrib |= FA_HIDDEN;
  49. #endif
  50.        if (!(mode & S_IWRITE))
  51.                dosattrib |= FA_RDONLY;
  52.        r = Fattrib(path, 1, dosattrib);
  53.        if (r < 0) {
  54. /* GEMDOS doesn't allow chmod on a directory, so pretend it worked */
  55.         if (dosattrib & FA_DIR)
  56.           return 0;
  57.                errno = -r;
  58.                return -1;
  59.        }
  60.        return 0;
  61. }
  62.  
  63. /*
  64.  * chown: this is faked if MiNT is not running
  65.  */
  66.  
  67. int chown(_name, uid, gid)
  68.        const char *_name;
  69.        int uid, gid;
  70. {
  71.     int r;
  72.     char name[PATH_MAX];
  73.  
  74.     if (__mint >= 9) {
  75.         (void)_unx2dos(_name, name);
  76.         r = (int)Fchown(name, uid, gid);
  77.         if (r && (r != -EINVAL)) {
  78.             errno = -r;
  79.             return -1;
  80.         }
  81.         return 0;
  82.     }
  83.     return 0;
  84. }
  85.